package com.jsh.erp.controller;

import com.jsh.erp.datasource.entities.Invoice;
import com.jsh.erp.datasource.entities.InvoiceItem;
import com.jsh.erp.datasource.vo.InvoiceVo;
import com.jsh.erp.exception.BusinessRunTimeException;
import com.jsh.erp.service.invoice.InvoiceService;
import com.jsh.erp.service.invoiceItem.InvoiceItemService;
import com.jsh.erp.utils.BaseResponseInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发票控制器
 *
 * 该控制器类用于处理与发票相关的请求，包括获取发票信息、添加、更新、删除发票等操作。
 *
 * @Author Yik
 * @Date 2025-01-08
 */
@RestController
@RequestMapping("/invoice")
@Api(tags = {"财务管理"})
public class InvoiceController {

    @Resource
    private InvoiceService invoiceService;


    @Resource
    private InvoiceItemService invoiceItemService;

    @GetMapping("/findDetailsById")
    public BaseResponseInfo findDetailsById(@RequestParam Map<String,Object> val){
        BaseResponseInfo info = new BaseResponseInfo();
        try{
            Long id = Long.valueOf(String.valueOf(val.get("invoiceId")));
            Assert.state(!ObjectUtils.isEmpty(id),"参数异常");
            List<InvoiceItem> result = invoiceItemService.findDetailsByInvoiceId(id);
            Assert.state(!CollectionUtils.isEmpty(result),"参数查询异常");
            info.code= 200;
            info.data= result;
        }catch (Exception e){
            throw new BusinessRunTimeException(500,e.getMessage());
        }
        return info;
    }

//    /**
//     * 添加新发票
//     * @param Invoice 发票实体
//     * @return 添加结果
//     */
//    @PostMapping("/add")
//    public int addInvoice(@RequestBody Invoice Invoice) {
//        return InvoiceService.addInvoice(Invoice);
//    }

//    /**
//     * 更新发票信息
//     * @param Invoice 发票实体
//     * @return 更新结果
//     */
//    @PutMapping("/update")
//    public int updateInvoice(@RequestBody Invoice Invoice) {
//        return InvoiceService.updateInvoice(Invoice);
//    }

    /**
     * 获取所有发票信息
     * @return 发票列表
     */
//    @GetMapping("/list")
//    public BaseResponseInfo getAllInvoices() {
//        BaseResponseInfo info = new BaseResponseInfo();
//
//        List<Invoice> allInvoices = null;
//        try {
//            allInvoices = InvoiceService.getAllInvoices();
//            if(!CollectionUtils.isEmpty(allInvoices)){
//                info.code = 200;
//                info.data = allInvoices;
//            }else{
//                throw new RuntimeException("为空~");
//            }
//        } catch (RuntimeException e) {
//            info.code = 500;
//            info.data = e.getMessage();
//        }
//        return info;
//    }

//    /**
//     * 根据ID获取单个发票信息
//     * @param id 发票ID
//     * @return 发票实体
//     */
//    @GetMapping("/{id}")
//    public Invoice getInvoiceById(@PathVariable Long id) {
//        return InvoiceService.getInvoiceById(id);
//    }
//
//    /**
//     * 删除发票
//     * @param id 发票ID
//     * @return 删除结果
//     */
//    @DeleteMapping("/delete/{id}")
//    public int deleteInvoice(@PathVariable Long id) {
//        return InvoiceService.deleteInvoice(id);
//    }
}
